(function() {
  var Feedback, FeedbackCollection, FriendshipRequest, FriendshipRequestCollection, LoggedUser, Message, MessageCollection, ROOT_URL, Skill, SkillCollection, SkillDatabase, StaticUserFunctions, User, UserCollection,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  ROOT_URL = "http://localhost:8080/swimpool/controller";

  window.ROOT_URL = ROOT_URL;

  User = (function(_super) {

    __extends(User, _super);

    function User() {
      this.promote = __bind(this.promote, this);

      this.demote = __bind(this.demote, this);

      this.idChanged = __bind(this.idChanged, this);

      this.url = __bind(this.url, this);
      return User.__super__.constructor.apply(this, arguments);
    }

    User.prototype.urlRoot = ROOT_URL + "/users";

    User.prototype.url = function() {
      var id;
      id = this.get("id");
      if (id && (id >= 0)) {
        return this.urlRoot + "/" + id;
      } else {
        return this.urlRoot;
      }
    };

    User.prototype.avatarUrl = function() {
      return this.url() + "/avatar";
    };

    User.prototype.initialize = function() {
      var _this = this;
      this.skills = new SkillCollection();
      this.skills.url = function() {
        return _this.url() + "/skills";
      };
      this.friends = new UserCollection();
      this.friends.url = function() {
        return _this.url() + "/friends";
      };
      this.feedbacks = new FeedbackCollection();
      this.feedbacks.url = function() {
        return _this.url() + "/feedbacks";
      };
      this.set("role", null);
      return this.on("change:id", this.idChanged);
    };

    User.prototype.copyFrom = function(user) {
      this.set("id", user.get("id"));
      this.set("name", user.get("name"));
      this.set("description", user.get("description"));
      this.set("information", user.get("information"));
      return this.set("email", user.get("email"));
    };

    User.prototype.idChanged = function(id) {
      var _this = this;
      this.id = this.get("id");
      this.skills.url = function() {
        return _this.url() + "/skills";
      };
      return this.friends.url = function() {
        return _this.url() + "/friends";
      };
    };

    User.prototype.demote = function(onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/admins/" + this.id;
      return $.ajax({
        type: "DELETE",
        contentType: "application/json",
        url: url,
        dataType: "*/*",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    User.prototype.promote = function(onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/admins/" + this.id;
      return $.ajax({
        type: "PUT",
        contentType: "application/json",
        url: url,
        dataType: "*/*",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    User.prototype.isAdmin = function() {
      var role;
      role = this.get("role");
      return (role === "Administrator") || (role === "SuperUser");
    };

    User.prototype.isSuperUser = function() {
      return this.get("role") === "SuperUser";
    };

    User.prototype.isVisitor = function() {
      return this.get("role") === null;
    };

    User.prototype.addSkill = function(skill, onSuccess, onError) {
      var sid, url;
      sid = skill.get("id");
      url = "" + (this.url()) + "/skills/" + sid;
      return $.ajax({
        type: "PUT",
        url: url,
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    User.prototype.deleteAvatar = function(onSuccess, onError) {
      var url;
      url = this.url() + "/avatar";
      return $.ajax({
        type: "DELETE",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    User.prototype.register = function(email, password, onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/users";
      this.set("email", email);
      return $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify({
          email: email,
          password: password
        }),
        contentType: "application/json",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    User.prototype.forcePassword = function(password, onSuccess, onError) {
      var url;
      url = this.url() + "/password";
      return $.ajax({
        type: "PUT",
        url: url,
        data: password,
        contentType: "text/plain",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    return User;

  })(Backbone.Model);

  StaticUserFunctions = (function() {

    function StaticUserFunctions() {}

    StaticUserFunctions.prototype.resetViaEmail = function(email, onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/self/reset/password?email=" + email;
      return $.ajax({
        type: "PUT",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    StaticUserFunctions.prototype.resetViaUsername = function(username, onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/self/reset/account?name=" + username;
      return $.ajax({
        type: "PUT",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    StaticUserFunctions.prototype.activate = function(id, onSuccess, onError) {
      var url;
      url = "" + ROOT_URL + "/accounts/" + id;
      return $.ajax({
        type: "PUT",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    StaticUserFunctions.prototype.login = function(email, password, onSuccess, onError) {
      var credentials, url;
      url = "" + ROOT_URL + "/login";
      credentials = {
        email: email,
        password: password
      };
      return $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(credentials),
        contentType: "application/json",
        dataType: "text/plain",
        success: onSuccess,
        error: function(xhr) {
          if (xhr.status === 200) {
            return onSuccess(xhr.responseText);
          } else {
            return onError(xhr);
          }
        }
      });
    };

    return StaticUserFunctions;

  })();

  UserCollection = (function(_super) {

    __extends(UserCollection, _super);

    function UserCollection() {
      return UserCollection.__super__.constructor.apply(this, arguments);
    }

    UserCollection.prototype.url = function() {
      return ROOT_URL + "/users";
    };

    UserCollection.prototype.model = User;

    return UserCollection;

  })(Backbone.Collection);

  LoggedUser = (function(_super) {

    __extends(LoggedUser, _super);

    function LoggedUser() {
      this.url = __bind(this.url, this);
      return LoggedUser.__super__.constructor.apply(this, arguments);
    }

    LoggedUser.prototype.urlRoot = ROOT_URL + "/self";

    LoggedUser.prototype.url = function() {
      return this.urlRoot;
    };

    LoggedUser.prototype.initialize = function() {
      LoggedUser.__super__.initialize.apply(this, arguments);
      this.messages = new MessageCollection();
      this.messages.url = ROOT_URL + "/self/messages";
      this.requests = new FriendshipRequestCollection();
      return this.requests.url = ROOT_URL + "/self/requests";
    };

    LoggedUser.prototype.save = function(attributes, options) {
      var putUrl;
      putUrl = this.url();
      if (attributes.name) {
        this.set("name", attributes.name);
      }
      if (attributes.description) {
        this.set("description", attributes.description);
      }
      if (attributes.information) {
        this.set("information", attributes.information);
      }
      return $.ajax({
        type: "PUT",
        contentType: "application/json",
        data: JSON.stringify(attributes),
        url: putUrl,
        dataType: "*/*",
        error: handleErrorCodes(options.error, options.success),
        success: options.success
      });
    };

    LoggedUser.prototype.removeFriend = function(friend, onSuccess, onError) {
      var id, url;
      id = friend.get("id");
      url = "" + ROOT_URL + "/self/friends/" + id;
      return $.ajax({
        type: "DELETE",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    LoggedUser.prototype.uploadAvatar = function(data, onSuccess, onError, onProgress) {
      var url;
      url = this.url() + "/avatar";
      return $.ajax({
        url: url,
        type: "PUT",
        data: data,
        xhr: function() {
          var myXhr;
          myXhr = $.ajaxSettings.xhr();
          if (myXhr.upload) {
            myXhr.upload.addEventListener('progress', onProgress, false);
          }
          return myXhr;
        },
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess),
        cache: false,
        contentType: false,
        processData: false
      });
    };

    LoggedUser.prototype.logout = function() {
      this.set("id", -1);
      this.set("name", "Visitor");
      this.set("description", null);
      this.set("information", null);
      return this.set("role", null);
    };

    return LoggedUser;

  })(User);

  Skill = (function(_super) {

    __extends(Skill, _super);

    function Skill() {
      return Skill.__super__.constructor.apply(this, arguments);
    }

    Skill.prototype.urlRoot = ROOT_URL + "/skills";

    Skill.prototype.propose = function(onSuccess, onError) {
      var attributes, description, name, options;
      this.url = function() {
        return "" + ROOT_URL + "/skills/proposals";
      };
      name = this.get("name");
      description = this.get("description");
      attributes = {
        name: name,
        description: description
      };
      options = {
        success: onSuccess,
        error: onError
      };
      return this.save(attributes, options);
    };

    Skill.prototype.approve = function(onSuccess, onError) {
      var id, url;
      id = this.get("id");
      url = "" + ROOT_URL + "/skills/proposals/" + id + "?accepted=true";
      return $.ajax({
        type: "DELETE",
        url: url,
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    Skill.prototype.decline = function(onSuccess, onError) {
      this.url = function() {
        return "" + ROOT_URL + "/skills/proposals";
      };
      return this.destroy();
    };

    return Skill;

  })(Backbone.Model);

  SkillCollection = (function(_super) {

    __extends(SkillCollection, _super);

    function SkillCollection() {
      return SkillCollection.__super__.constructor.apply(this, arguments);
    }

    SkillCollection.prototype.model = Skill;

    SkillCollection.prototype.getById = function(id) {
      var match;
      match = this.where({
        id: Number(id)
      });
      if (!match || match.length === 0) {
        return null;
      }
      return match[0];
    };

    return SkillCollection;

  })(Backbone.Collection);

  SkillDatabase = (function(_super) {

    __extends(SkillDatabase, _super);

    function SkillDatabase() {
      SkillDatabase.__super__.constructor.apply(this, arguments);
      this.lastUpdate = 0;
      this.minUpdateInterval = 5000;
    }

    SkillDatabase.prototype.url = function() {
      return ROOT_URL + "/skills";
    };

    SkillDatabase.prototype.isEmpty = function() {
      return this.length === 0;
    };

    return SkillDatabase;

  })(SkillCollection);

  Feedback = (function(_super) {

    __extends(Feedback, _super);

    function Feedback() {
      return Feedback.__super__.constructor.apply(this, arguments);
    }

    Feedback.prototype.urlRoot = ROOT_URL + "/feedbacks";

    return Feedback;

  })(Backbone.Model);

  FeedbackCollection = (function(_super) {

    __extends(FeedbackCollection, _super);

    function FeedbackCollection() {
      return FeedbackCollection.__super__.constructor.apply(this, arguments);
    }

    FeedbackCollection.prototype.model = Feedback;

    return FeedbackCollection;

  })(Backbone.Collection);

  Message = (function(_super) {

    __extends(Message, _super);

    function Message() {
      return Message.__super__.constructor.apply(this, arguments);
    }

    Message.prototype.sendToId = function(id, onSuccess, onError) {
      var text, url;
      text = this.get("text");
      url = "" + ROOT_URL + "/users/" + id + "/messages";
      return $.ajax({
        type: "POST",
        contentType: "text/plain",
        dataType: "*/*",
        url: url,
        data: text,
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    Message.prototype.sendTo = function(user, onSuccess, onError) {
      var id;
      id = user.get("id");
      return this.sendToId(id, onSuccess, onError);
    };

    return Message;

  })(Backbone.Model);

  MessageCollection = (function(_super) {

    __extends(MessageCollection, _super);

    function MessageCollection() {
      return MessageCollection.__super__.constructor.apply(this, arguments);
    }

    MessageCollection.prototype.model = Message;

    return MessageCollection;

  })(Backbone.Collection);

  FriendshipRequest = (function(_super) {

    __extends(FriendshipRequest, _super);

    function FriendshipRequest() {
      return FriendshipRequest.__super__.constructor.apply(this, arguments);
    }

    FriendshipRequest.prototype.send = function(onSuccess, onError) {
      var target, url;
      target = this.get("target");
      url = "" + (target.url()) + "/requests";
      return $.ajax({
        type: "POST",
        url: url,
        data: "",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    FriendshipRequest.prototype.accept = function(onSuccess, onError) {
      var id, url;
      id = this.getId();
      url = "" + ROOT_URL + "/self/requests/" + id + "?accepted=true";
      return $.ajax({
        type: "DELETE",
        url: url,
        dataType: "*/*",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    FriendshipRequest.prototype.decline = function(onSuccess, onError) {
      var id, url;
      id = this.getId();
      url = "" + ROOT_URL + "/self/requests/" + id;
      return $.ajax({
        type: "DELETE",
        url: url,
        dataType: "*/*",
        success: onSuccess,
        error: handleErrorCodes(onError, onSuccess)
      });
    };

    FriendshipRequest.prototype.getId = function() {
      var sender;
      sender = this.get("sender");
      if (sender.get) {
        return sender.get("id");
      }
      return sender.id;
    };

    FriendshipRequest.prototype.getSenderName = function() {
      var sender;
      sender = this.get("sender");
      if (sender.get) {
        return sender.get("name");
      }
      return sender.name;
    };

    return FriendshipRequest;

  })(Backbone.Model);

  FriendshipRequestCollection = (function(_super) {

    __extends(FriendshipRequestCollection, _super);

    function FriendshipRequestCollection() {
      return FriendshipRequestCollection.__super__.constructor.apply(this, arguments);
    }

    FriendshipRequestCollection.prototype.model = FriendshipRequest;

    return FriendshipRequestCollection;

  })(Backbone.Collection);

  window.model = {
    User: User,
    Skill: Skill,
    Feedback: Feedback,
    Message: Message,
    FriendshipRequest: FriendshipRequest,
    LoggedUser: LoggedUser,
    SkillDatabase: SkillDatabase
  };

  window.utils = {
    User: new StaticUserFunctions()
  };

  window.collection = {
    User: UserCollection,
    Skill: SkillCollection,
    Feedback: FeedbackCollection
  };

  window.targetUser = new User();

  window.currentUser = new LoggedUser();

  window.loadPersonalInfo = function() {
    return window.currentUser.fetch({
      success: function() {
        window.targetUser.copyFrom(window.currentUser);
        $("#homeLabel").text(currentUser.get("name"));
        if (currentUser.isAdmin()) {
          return $("#showProposals").show();
        } else {
          return $("#showProposals").hide();
        }
      }
    });
  };

}).call(this);
